package com.gps.skyrc.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gps.skyrc.R;
import com.gps.skyrc.api.Config;
import com.gps.skyrc.bean.DeviceInfo;
import com.gps.skyrc.port.OnlyClickListener;

import java.util.List;

/**
 * Create by ywq
 * data 2018/12/4
 */
public class ShowNameAdapter extends RecyclerView.Adapter<ShowNameAdapter.ShowNameViewHolder> {

    private Context mContext;
    private List<DeviceInfo> mInfos;
    private OnlyClickListener mListener;

    public ShowNameAdapter(Context context, List<DeviceInfo> infos, OnlyClickListener clickListener) {
        this.mContext = context;
        this.mInfos = infos;
        this.mListener = clickListener;
    }

    @NonNull
    @Override
    public ShowNameViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = View.inflate(mContext, R.layout.name_item, null);
        return new ShowNameViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ShowNameViewHolder holder, int position) {
        DeviceInfo bean = mInfos.get(position);
        if (bean.getName() != null && !bean.getName().equals(""))
            holder.mNameTv.setText(bean.getName());
        else {
            String mac = bean.getMac().substring(12).replace(":","");
            holder.mNameTv.setText(Config.BLE_FIRST_CONNECT + "-" + mac);
        }
        if (mListener != null)
            holder.mItemRl.setOnClickListener(v -> mListener.onItemClick(bean,position));
    }

    @Override
    public int getItemCount() {
        return mInfos == null ? 0 : mInfos.size();
    }

    class ShowNameViewHolder extends RecyclerView.ViewHolder {

        private final TextView mNameTv;
        private final RelativeLayout mItemRl;

        public ShowNameViewHolder(@NonNull View itemView) {
            super(itemView);
            mNameTv = itemView.findViewById(R.id.name_tv);
            mItemRl = itemView.findViewById(R.id.item_rl);

        }
    }
}
